Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

198
Views
What is the purpose of "?" in (someDelegateName)?.Invoke();?

The following code snippet was highlighted in Visual Studio alongside a suggestion for simplifying it.

if ( drawMethodsDelegate != null )
    drawMethodsDelegate ( e.Graphics );

When I clicked on the lightbulb suggestion thing, Visual Studio refactored it into the following

drawMethodsDelegate?.Invoke ( e.Graphics );

And no. The question mark is not a typo. I don't understand what the question mark is used for and I can't find anything relevant on MSDN. I also looked at the Tutorial Point Delegates page but found no useful information.

Tutorial Point page , MSDN Delegates page , MSDN Control.Invoke page

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

This is the null conditional operator.

drawMethodsDelegate?.Invoke ( e.Graphics );

Provided that drawMethodsDelegate is not null calls the Invoke method. It is an operator that being introduced in the 6th version of C# and you can see it as a syntactic sugar, which helps you to write less code for handling null checks.

Last but not least, the above check is also thread-safe !

For further info please have a look here

over 4 years ago · Santiago Trujillo Report

0

This is a null condition operator that came with C# 6.0

https://msdn.microsoft.com/en-us/library/dn986595.aspx

it means IF drawMethodsDelegate is not null Invoke the method else do nothing.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!